package cn.leyaber.hrm.service.impl;

import cn.leyaber.hrm.domain.*;
import cn.leyaber.hrm.mapper.*;
import cn.leyaber.hrm.quartz.server.IQuartzService;
import cn.leyaber.hrm.service.IOrderGoodsService;
import cn.leyaber.hrm.util.CodeGenerateUtils;
import cn.leyaber.hrm.util.DistanceUtil;
import cn.leyaber.hrm.util.Point;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ma.rui.kai
 * @since 2020-10-28
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper,OrderGoods> implements IOrderGoodsService {
    //产品订单
    @Resource
    private OrderGoodsMapper orderGoodsMapper;
    //用户地址
    @Resource
    private AddressMapper addressMapper;
    //商品
    @Resource
    private  ProductMapper productMapper;
    //商品明细订单
    private OrderGoodsDetailMapper orderGoodsDetailMapper;
    //时间设置
    @Resource
    private IQuartzService quartzService;

    @Override
    @Transactional
    public String submit(Map<String, Object> params, Wholename wholename) {
    return null;
    }




    @Override
    public void cancelOrder(long orderId) {
        //产品订单对象
        OrderGoods orderGoods = orderGoodsMapper.selectById(orderId);
        if (orderGoods.getState() != -1) {
            orderGoods.setState(-1);
            orderGoodsMapper.updateById(orderGoods);
        }
    }
   @Resource
    private ShopMapper shopMapper;
    private OrderGoods getOrderGoods(Map< String,Object> params, Address address, Product product, Wholename wholename){
        long product_num = Long.parseLong(params.get("product_num").toString());
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setDigest("[服务订单]购买"+product_num+"次"+product.getName()+"服务!");
        orderGoods.setState(0);
        BigDecimal multiply = product.getSaleprice().multiply(new BigDecimal(100))
                .multiply(new BigDecimal(product_num));
        orderGoods.setPrice(multiply);
        orderGoods.setOrderSn(CodeGenerateUtils.generateOrderSn(wholename.getId()));
        Date date = new Date(System.currentTimeMillis() + 60 * 1000);
        orderGoods.setLastPayTime(date);
        orderGoods.setGoodsId(wholename.getId());
        List<Shop> shops = shopMapper.selectList(null);
        Point point = DistanceUtil.getPoint(address.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point,shops);
       orderGoods.setShopId(nearestShop.getId());
       orderGoods.setPaySn(CodeGenerateUtils.generateUnionPaySn());
       return  orderGoods;
    }
}

